// 景点管理页面
const app = getApp();
const request = require('../../../../utils/request.js').request;

Page({
  data: {
    scenicAreaId: null,
    scenicAreaName: '',
    loading: true,
    spots: [],
    showAddForm: false,
    showEditForm: false,
    currentSpot: null,
    formData: {
      id: '',
      scenicAreaId: '',
      name: '',
      description: ''
    }
  },

  onLoad: function (options) {
    // 获取URL参数
    const { scenicAreaId, scenicAreaName } = options;
    
    this.setData({
      scenicAreaId,
      scenicAreaName
    });
    
    // 加载景点列表
    this.loadScenicSpots();
  },

  onPullDownRefresh: function () {
    this.loadScenicSpots();
  },

  // 加载景点列表
  loadScenicSpots: function () {
    this.setData({ loading: true });
    
    request({
      url: `/api/scenicspot/scenic/${this.data.scenicAreaId}`,
      method: 'GET',
      success: (res) => {
        if (res.code === 0) {
          this.setData({
            spots: res.data,
            loading: false
          });
        } else {
          wx.showToast({
            title: res.msg || '获取景点列表失败',
            icon: 'none'
          });
          this.setData({ loading: false });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
        this.setData({ loading: false });
      },
      complete: () => {
        wx.stopPullDownRefresh();
      }
    });
  },

  // 显示添加表单
  showAddForm: function () {
    this.setData({
      showAddForm: true,
      formData: {
        id: '',
        scenicAreaId: this.data.scenicAreaId,
        name: '',
        description: ''
      }
    });
  },

  // 显示编辑表单
  showEditForm: function (e) {
    const index = e.currentTarget.dataset.index;
    const spot = this.data.spots[index];
    
    this.setData({
      showEditForm: true,
      currentSpot: spot,
      formData: {
        id: spot.id,
        scenicAreaId: this.data.scenicAreaId,
        name: spot.name,
        description: spot.description || ''
      }
    });
  },

  // 关闭表单
  closeForm: function () {
    this.setData({
      showAddForm: false,
      showEditForm: false
    });
  },

  // 输入表单数据
  inputChange: function (e) {
    const { field } = e.currentTarget.dataset;
    let { formData } = this.data;
    formData[field] = e.detail.value;
    this.setData({ formData });
  },

  // 提交添加表单
  submitAddForm: function () {
    const { formData } = this.data;
    
    if (!formData.name) {
      wx.showToast({
        title: '景点名称不能为空',
        icon: 'none'
      });
      return;
    }
    
    // 构建正确的请求数据结构，确保与后端接口匹配
    const requestData = {
      name: formData.name,
      description: formData.description,
      scenicArea: {
        id: parseInt(formData.scenicAreaId)
      }
    };
    
    request({
      url: '/api/scenicspot/add',
      method: 'POST',
      data: requestData,
      success: (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '添加成功',
            icon: 'success'
          });
          this.setData({ showAddForm: false });
          this.loadScenicSpots();
        } else {
          wx.showToast({
            title: res.msg || '添加失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  // 提交编辑表单
  submitEditForm: function () {
    const { formData } = this.data;
    
    if (!formData.name) {
      wx.showToast({
        title: '景点名称不能为空',
        icon: 'none'
      });
      return;
    }
    
    // 构建正确的请求数据结构，确保与后端接口匹配
    const requestData = {
      name: formData.name,
      description: formData.description,
      scenicArea: {
        id: parseInt(formData.scenicAreaId)
      }
    };
    
    request({
      url: `/api/scenicspot/${formData.id}`,
      method: 'PUT',
      data: requestData,
      success: (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '更新成功',
            icon: 'success'
          });
          this.setData({ showEditForm: false });
          this.loadScenicSpots();
        } else {
          wx.showToast({
            title: res.msg || '更新失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  // 删除景点
  deleteSpot: function (e) {
    const index = e.currentTarget.dataset.index;
    const spot = this.data.spots[index];
    
    wx.showModal({
      title: '确认删除',
      content: `确定要删除景点"${spot.name}"吗？`,
      success: (res) => {
        if (res.confirm) {
          request({
            url: `/api/scenicspot/${spot.id}`,
            method: 'DELETE',
            success: (res) => {
              if (res.code === 0) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                this.loadScenicSpots();
              } else {
                wx.showToast({
                  title: res.msg || '删除失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  }
}) 